$(function () {
	$("#jqGrid").jqGrid({
		url: baseURL + 'work/experiment/list',
		datatype: "json",
		colModel: [
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true,hidden:true },
			{ label: '实验名字', name: 'exName', index: 'ex_name', width: 80 },
			{ label: '实验内容', name: 'exContent', index: 'ex_content', width: 80 },
			{ label: '院系', name: 'exDepartment', index: 'ex_department', width: 80 },
			{ label: '实验人', name: 'exStudent', index: 'ex_student', width: 80 },
			{ label: '实验时间', name: 'exTime', index: 'ex_time', width: 80 },
			{ label: '实验房间', name: 'exRoom', index: 'ex_room', width: 80 },
			{ label: '实验成绩', name: 'exScore', index: 'ex_score', width: 80 },
			{ label: '实验编号', name: 'uuid', index: 'uuid', width: 80,hidden:true },
			{ label: '开始时间', name: 'exStarttime', index: 'ex_starttime', width: 80 },
			{ label: '结束时间', name: 'exEndtime', index: 'ex_endtime', width: 80 },
			{ label: 'openid', name: 'openid', index: 'openid', width: 80,hidden:true },
			{
				label: '通过', name: 'su_status', width: 60, formatter: function (value, options, row) {
					return row.exStatus == 1 ?
						'<span class="label label-danger">通过</span>' :
						'<span class="label label-success">否决</span>';
				}
			}
		],
		viewrecords: true,
		height: 385,
		rowNum: 10,
		rowList : [10,30,50],
		rownumbers: true,
		rownumWidth: 25,
		autowidth:true,
		multiselect: true,
		pager: "#jqGridPager",
		jsonReader : {
			root: "page.list",
			page: "page.currPage",
			total: "page.totalPage",
			records: "page.totalCount"
		},
		prmNames : {
			page:"page",
			rows:"limit",
			order: "order"
		},
		gridComplete:function(){
			//隐藏grid底部滚动条
			$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
		}
	});
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		experiment: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.experiment = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
			vm.title = "修改";

			vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.experiment.id == null ? "work/experiment/save" : "work/experiment/update";
			$.ajax({
				type: "POST",
				url: baseURL + url,
				contentType: "application/json",
				data: JSON.stringify(vm.experiment),
				success: function(r){
					if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}

			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
					url: baseURL + "work/experiment/delete",
					contentType: "application/json",
					data: JSON.stringify(ids),
					success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "work/experiment/info/"+id, function(r){
				vm.experiment = r.experiment;
			});
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
				page:page
			}).trigger("reloadGrid");
		}
	}
});